package com.myteam.fugaocrm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import com.myteam.fugaocrm.modle.FgStaff;
import com.myteam.fugaocrm.modle.FgUser;
import com.myteam.fugaocrm.sqldb.mapper.FgStaffMapper;
import com.myteam.fugaocrm.sqldb.mapper.FgUserMapper;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SelfController {
	@Resource protected FgStaffMapper fgStaffMapper;
	@Resource protected FgUserMapper fgUserMapper;
	
	@RequestMapping("self/msg")
	@ResponseBody
	public ModelAndView msg(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer staffId = Integer.parseInt(request.getSession().getAttribute("staffId").toString());
		//查询员工
		FgStaff staff = fgStaffMapper.selectStaffByStaffId(staffId);
		//查询用户
		FgUser user = fgUserMapper.selectUserByUserId(staff.getUserId());
		
		ModelAndView view = new ModelAndView();
		view.addObject("userName", user.getUserName());
		view.addObject("staffName", staff.getStaffName());
		view.addObject("staffMobile", staff.getStaffMobile());
		view.setViewName("biz_user_edit.jsp");
		return view;
	}
	
	@RequestMapping("self/insertorupdate")
	@ResponseBody
	public ModelAndView insertorupdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer staffId = Integer.parseInt(request.getSession().getAttribute("staffId").toString());
		String staffMobile = request.getParameter("staffMobile");
		
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("staffId", staffId);
		param.put("staffMobile", staffMobile);
		param.put("modifiedUser", staffId);
		param.put("modifiedDate", new Date());
		
		fgStaffMapper.updateMobile(param);
		
		ModelAndView view = new ModelAndView();
		view.setViewName("redirect:/self/msg");
		return view;
	}
	
	@RequestMapping("self/password")
	@ResponseBody
	public ModelAndView password(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView view = new ModelAndView();
		view.setViewName("biz_password_edit.jsp");
		return view;
	}
	
	@RequestMapping("self/updatepassword")
	@ResponseBody
	public ModelAndView updatePassword(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer staffId = Integer.parseInt(request.getSession().getAttribute("staffId").toString());
		String password = request.getParameter("password");
		String newPassword = request.getParameter("newPassword");
//		String newPassword2 = request.getParameter("newPassword2");
		
		//校验
		
		//查询员工
		FgStaff staff = fgStaffMapper.selectStaffByStaffId(staffId);
		//查询用户
		FgUser user = fgUserMapper.selectUserByUserId(staff.getUserId());
		
		if(user.getPassword().equals(password)) {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("userId", user.getUserId());
			param.put("password", newPassword);
			fgUserMapper.updatePassword(param);
		}
		
		ModelAndView view = new ModelAndView();
		view.setViewName("index.jsp");
		return view;
	}
	
	/**
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("self/resetpassword")
	@ResponseBody
	public String resetPassword(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer staffId = Integer.parseInt(request.getParameter("staffId").toString());
		//查询员工
		FgStaff staff = fgStaffMapper.selectStaffByStaffId(staffId);
		
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("userId", staff.getUserId());
		param.put("password", "000000");
		fgUserMapper.updatePassword(param);
		
		return "重置密码成功";
	}
}
